# 检查 有离群点的列，结果发现：SkinThickness  与 Insulin 两列存在离群点
import numpy as np
import pandas as pd

df = pd.read_csv("../DATA/diabetes.csv")

# 将0替换成NaN
df[['Glucose','BloodPressure','SkinThickness','Insulin','BMI']] = df[['Glucose','BloodPressure','SkinThickness','Insulin','BMI']].replace(0, np.NaN)

def carp(x,y):
    z = x*y
    return z
# 查看某一列的，按照outcome分组的中位数
def median_target(var):
    temp = df[df[var].notnull()]
    temp = temp[[var, 'Outcome']].groupby(['Outcome'])[[var]].median().reset_index()
    return temp

columns = df.columns
columns = columns.drop("Outcome")

# 先根据Outcome进行分组，然后进行中位数填充
for col in columns:
    df.loc[(df['Outcome'] == 0 ) & (df[col].isnull()), col] = median_target(col)[col][0]
    df.loc[(df['Outcome'] == 1 ) & (df[col].isnull()), col] = median_target(col)[col][1]

df.loc[(df['Outcome'] == 0 ) & (df["Pregnancies"].isnull()), "Pregnancies"]
df[(df['Outcome'] == 0 ) & (df["BloodPressure"].isnull())]

y = df["Outcome"]
X = df.drop(["Outcome"],axis = 1)
cols = X.columns
index = X.index

from sklearn.preprocessing import RobustScaler
transformer = RobustScaler().fit(X)
X = transformer.transform(X)
X = pd.DataFrame(X, columns = cols, index = index)

scaler_result = pd.DataFrame(X,y)

scaler_result.to_csv('./data_scalered.csv')